<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/01/26
 * Time: 下午 10:35
 */

namespace app\admin\controller;


class Vehicle extends Common
{
    public function index()
    {
        $data = [];
        // 搜索
        $title = input('post.title');
        $data['title'] = $title;
        //页码
        $pageNum = 1;
        if (is_numeric(input('post.pageNum'))) {
            $pageNum = input('post.pageNum');
        }
        $data['pageNum'] = $pageNum;
        //每页数量
        $numPerPage = config('num_per_page');
        $data['numPerPage'] = $numPerPage;
        $Vehicle = new \app\admin\model\Vehicle();
        $list = $Vehicle->getVehicleList($pageNum,$numPerPage,$title);
        $count = $Vehicle->getVehicleCount($title);
        $data['list'] = $list;
        $data['count'] = $count;
        return view('',$data);
    }

    public function vehicleAdd()
    {
        return view('vehicleadd');
    }

    public function doVehicleAdd()
    {
        $data = input('post.');
        try {
            $vehicle = new \app\admin\model\Vehicle();
            $vehicle->doAddVehicle($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '添加成功',
            'navTabId' => 'vehicleManage',
            'callbackType' => 'closeCurrent',
        ]);
    }

    public function vehicleEdit()
    {
        $id = input('get.id');
        $vehicle = db("vehicle")
                ->where("id",$id)
                ->find();
        return view("vehicleedit",['vehicle'=>$vehicle]);
    }

    public function doVehicleEdit()
    {
        $data = input('post.');
        try {
            $vehicle = new \app\admin\model\Vehicle();
            $vehicle->doModifyVehicle($data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '修改成功',
            'navTabId' => 'vehicleManage',
            'callbackType' => 'closeCurrent',
        ]);
    }

    public function vehicleDel()
    {
        $id = input("get.id");
        try {
            $vehicle = new \app\admin\model\Vehicle();
            $vehicle->delVehicle($id);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message' => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message' => '删除成功',
            'navTabId' => 'SysmsgManage',
        ]);
    }
}